<?
$id = $db->EscapeString($_GET['edit']);
$del = $db->EscapeString($_GET['del']);
if($del != "" && is_numeric($del)){$db->Query("DELETE FROM `users` WHERE `id`='".$del."'");}

$edit = $db->FetchArray($db->Query("SELECT * FROM `users` WHERE `id`='".$id."'"));
$twt = $db->FetchArray($db->Query("SELECT * FROM `twitter` WHERE `user`='".$id."' AND `active`='0'"));

if(isset($_POST['submit'])){
	$pass = md5($_POST['pass']);
	$value = ($_POST['pass'] != '' ? ", `pass`='".$pass."'" : '');
	
	$name = $db->EscapeString($_POST['username']);
	$email = $db->EscapeString($_POST['email']);
	$coins = $db->EscapeString($_POST['coins']);
	$status = $db->EscapeString($_POST['status']);
	$premium = $db->EscapeString($_POST['premium']);
	
	$db->Query("UPDATE `users` SET `login`='".$name."', `email`='".$email."'".$value.", `coins`='".$coins."', `premium`='".$premium."', `banned`='".$status."' WHERE `id`='".$id."'");
	$mesaj = '<div class="message success"><h3>Success!</h3><p>User was successfuly edited!</p></div>';
}

$users = $db->GetNumRows($db->Query("SELECT * FROM `users`"));
$banned = $db->GetNumRows($db->Query("SELECT * FROM `users` WHERE `banned`='1'"));
$active = $db->GetNumRows($db->Query("SELECT * FROM `users` WHERE `banned`='0'"));
?>
            <div id="page-subheader">
                <div class="wrapper clearfix">
                    <nav id="sub-nav">
                        <ul>
                            <li class="active"><a href="index.php?x=users">Users</a></li>
							<li><a href="index.php?x=search">Search</a></li>
                        </ul>
                    </nav>
                </div>
            </div>
        </header>
        
        <section id="content">
            <div class="wrapper">
                <section class="grid_6 first top">
                    <div class="columns leading">
                        <div class="grid_6 first"><? if($edit['login'] != ""){ echo $mesaj;?>
						<form id="form" method="post" class="form widget">
                                <header><h2>Edit User</h2></header>
                                <section>                 
                                    <fieldset>
                                         <dl>
                                             <dt></dt><dd><label>Username</label><input type="text" name="username" value="<?=(isset($_POST['username']) ? $_POST['username'] : $edit['login'])?>" required="required" /></dd>
											 <dt></dt><dd><label>Email</label><input type="email" name="email" value="<?=(isset($_POST['email']) ? $_POST['email'] : $edit['email'])?>" required="required" /></dd>
                                             <dt></dt><dd><label>Password</label><input type="text" name="pass" value="" /></dd>
											 <dt></dt><dd><label>Premium</label><input type="text" name="premium" value="<?=(isset($_POST['premium']) ? $_POST['premium'] : $edit['premium'])?>" /></dd>
                                             <dt></dt><dd><label>Coins</label><input type="text" name="coins" value="<?=(isset($_POST['coins']) ? $_POST['coins'] : $edit['coins'])?>" /></dd>
											 <dt></dt><dd><label>Status</label><select name="status"><option value="0">Active</option><option value="1"<?=($edit['banned'] != 0 ? ' selected' : '')?>>Banned</option></select></dd>
                                     	</dl>    
                                     </fieldset>
                                     <hr />
                                     <button class="button button-green" type="submit" name="submit">Submit</button>
                                     <button class="button button-gray" type="reset">Reset</button>
                                </section>
                            </form><?}else{?>
                            <table class="datatable sortable selectable paginate full">
                                <thead>
                                    <tr>
                                        <th width="25">ID</th>
                                        <th>Username</th>
                                        <th>Email</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
								<tfoot>
                                    <tr>
                                        <th width="25">ID</th>
                                        <th>Username</th>
                                        <th>Email</th>
                                        <th>Actions</th>
                                    </tr>
                                </tfoot>
                                <tbody>
<?
  $sql = $db->Query("SELECT id,login,email FROM `users` ORDER BY `id` ASC");
  for($j=1; $user = $db->FetchArray($sql); $j++)
{
?>	
                                    <tr>
                                        <td><?=$user['id']?></td>
                                        <td><?=$user['login']?></td>
                                        <td><?=$user['email']?></td>
                                        <td><ul class="action-buttons">
                                        <li><a href="index.php?x=users&edit=<?=$user['id']?>" class="action-button" title="Edit User"><span class="user-edit"></span></a></li>
										<li><a href="index.php?x=users&del=<?=$user['id']?>" class="action-button" title="Delete User"><span class="user-delete"></span></a></li></ul></td>
                                    </tr>
<?}?>
                                </tbody>
                            </table><?}?>
                        </div>
                    </div>

                    <div class="clear">&nbsp;</div>
                </section>

                <aside class="grid_2 top">
                    <div class="accordion">
                        <header class="current"><h2>Users</h2></header>
                        <section style="display:block">
                            <dl>
                                <dt>Active Users (<?=$active?>)</dt>
                                <dd><div class="progress progress-green"><span style="width: <?= @percent($active, $users)?>%;"><b><?= @percent($active, $users)?>%</b></span></div></dd>
								<dt>Banned Users (<?=$banned?>)</dt>
                                <dd><div class="progress progress-red"><span style="width: <?= @percent($banned, $users)?>%;"><b><?= @percent($banned, $users)?>%</b></span></div></dd>
                            </dl>
                        </section>
                    </div>
                </aside>

                <div class="clear"></div>

            </div>
            <div id="push"></div>
        </section>

    </div>
    
    <footer id="page-footer">
        <div id="footer-inner">
            <p class="wrapper">All rights reserved &copy; <a href="http://mafianet.org" target="_blank">MafiaNet</a> <?=date('Y')?></p>
        </div>
    </footer>
</body>
</html>